﻿using Forum.Model.Entity;
using Forum.Model.Response;
using Forum.Repository.Interface;
using Forum.Service.Interface;

namespace Forum.Service.Service
{
    public class BlogService : IBlogService
    {
        private readonly IAppRepository<Post> _postRepo;
        private readonly IAppRepository<Category> _categoryRepo;
        private readonly IAppRepository<Photo> _photoRepo;
        private readonly IAppRepository<TopPost> _topPostRepo;
        private readonly IAppRepository<FeaturedPost> _fPostRepo;
        private readonly IAppRepository<Notice> _noticeRepo;

        public BlogService(IAppRepository<Post> postRepo,
            IAppRepository<Category> categoryRepo,
            IAppRepository<Photo> photoRepo,
            IAppRepository<TopPost> topPostRepo,
            IAppRepository<FeaturedPost> fPostRepo,
            IAppRepository<Notice> noticeRepo)
        {
            _postRepo = postRepo;
            _categoryRepo = categoryRepo;
            _photoRepo = photoRepo;
            _topPostRepo = topPostRepo;
            _fPostRepo = fPostRepo;
            _noticeRepo = noticeRepo;
        }

        /// <summary>
        /// 获取博客信息概况
        /// </summary>
        /// <returns></returns>
        public BlogOverviewResp Overview()
        {
            return new BlogOverviewResp
            {
                PostsCount = _postRepo.Count(),
                CategoriesCount = _categoryRepo.Count(),
                PhotosCount = _photoRepo.Count(),
                FeaturedPostsCount = _fPostRepo.Count(),
                NoticeCount = _noticeRepo.Count()
            };
        }

        /// <summary>
        /// 查询推荐博客
        /// </summary>
        /// <returns></returns>
        public List<Post> GetFeaturedPosts()
        {
            return _fPostRepo.Db.Queryable<FeaturedPost>().Includes(a => a.Post.Category)
                .ToList(a => a.Post);
        }

        /// <summary>
        /// 添加推荐博客
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public FeaturedPost AddFeaturedPost(Post post)
        {
            var item = _fPostRepo.FindEntity(a => a.PostId == post.Id);
            if (item != null) return item;
            item = new FeaturedPost { PostId = post.Id };
            _fPostRepo.Insert(item);

            //文章是否置顶更新
            post.IsRecommend = true;
            _postRepo.Update(post);

            return item;
        }

        /// <summary>
        /// 删除推荐博客
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public bool DeleteFeaturedPost(Post post)
        {
            //文章是否置顶更新
            post.IsRecommend = false;
            _postRepo.Update(post);

            var item = _fPostRepo.FindEntity(a => a.PostId == post.Id);
            return item != null && _fPostRepo.Delete(item);
        }

        /// <summary>
        /// 查询置顶博客
        /// </summary>
        /// <returns></returns>
        public Post? GetTopOnePost()
        {
            return _topPostRepo.SugarQueryable.Includes(a => a.Post, p => p.Category)?.First()?.Post;
        }

        /// <summary>
        /// 设置置顶博客
        /// </summary>
        /// <param name="post"></param>
        /// <returns>返回 <see cref="TopPost"/> 对象和删除原有置顶博客的行数</returns>
        public (TopPost, int) SetTopPost(Post post)
        {
            var rows = _topPostRepo.Delete();
            var item = new TopPost { PostId = post.Id };
            _topPostRepo.Insert(item);

            //文章是否置顶更新
            post.IsTop = true;
            _postRepo.Update(post);

            return (item, rows);
        }

        /// <summary>
        /// 取消置顶
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public bool DeleteTopPost(Post post)
        {
            //文章是否置顶更新
            post.IsTop = false;
            _postRepo.Update(post);

            var item = _fPostRepo.FindEntity(a => a.PostId == post.Id);
            return item != null && _fPostRepo.Delete(item);
        }

        /// <summary>
        /// 查询时间线
        /// </summary>
        /// <returns></returns>
        public List<TimeLineResp> GetTimeLines()
        {
            var postList = _postRepo.SugarQueryable.Select(a => new TimeLineResp
            {
                contentId = a.Id,
                content = a.Title,
                timestamp = a.CreationTime.ToString("yyyy-MM-dd HH:mm"),
                orderTime = a.CreationTime,
                year = a.CreationTime.Year,
                month = a.CreationTime.Month
            }).ToList();

            var yearPost = postList.GroupBy(a => new { year = a.year, month = a.month }).Select(a => new { year = a.Key.year, month = a.Key.month }).ToList();
            foreach (var item in yearPost)
            {
                postList.Add(new TimeLineResp
                {
                    content = $"{item.year}-{item.month}",
                    orderTime = new DateTime(item.year, item.month, 1).AddMonths(1).AddSeconds(-1),
                    size = "large",
                    color = "#409EFF"
                });
            }

            return postList.OrderByDescending(a => a.orderTime).ToList();
        }
    }
}
